CCPARAM = -m64 -lstdc++  -Iinclude -nostdlib -fno-builtin -fpermissive -mcmodel=large  -fno-rtti -fno-exceptions -fno-leading-underscore
ASPARAM = -b elf64-x86-64 -static

# -Iinclude -fno-use-cxa-atexit -nostdlib -fno-builtin -fno-rtti -fno-exceptions -fno-leading-underscore
CC = g++
NASM = nasm
YASM = as
PIC := APIC
# SIMULATION := BOCHS
DEBUG = -g
objects = obj/kernel/head.o \
		  obj/kernel/kernel.o \
		  obj/kernel/log/log.o \
		  obj/kernel/interrupt/interrupt.o \
		  obj/kernel/interrupt/interruptproc.o \
		  obj/kernel/memory/memory.o \
		  obj/kernel/driver/keyboarddriver.o \
		  obj/kernel/task/task.o \
		  obj/kernel/task/taskproc.o \
		  obj/kernel/smp/cpu.o \
		  obj/libs/lib.o \
		  obj/pic.o \
		  obj/kernel/driver/mousedriver.o \
		  obj/kernel/driver/disk.o \
		  obj/kernel/driver/device.o \
		  obj/kernel/smp/smp.o \
		  obj/kernel/smp/APU_boot.o \
		  obj/libs/spinlock.o \
		  obj/kernel/timer/time.o \
		  obj/kernel/apic/ipi.o \
		  obj/kernel/vfs/fat32.o \
		  obj/kernel/vfs/vfs.o \
		  obj/kernel/syscall/syscall.o \
		  obj/kernel/syscall/sys.o \
		  obj/kernel/syscall/user.o \
		  obj/libs/waitqueue.o \
		  obj/libs/string.o \
		  obj/libs/path.o \
		  obj/kernel/driver/diskProc.o \
		  obj/kernel/timer/pit.o
		 

TESTOBJECT = \
			obj/kernel/headtest.o \
		    obj/kernel/testkernel.o


bins = obj/bootloader/boot.bin \
	   obj/bootloader/loader.bin

obj/%.o : src/%.cpp
	mkdir -p $(@D)

	${CC} ${CCPARAM} $(DEBUG) -D${PIC}   -c $< -o $@

ifeq ($(PIC),APIC)
obj/pic.o: src/kernel/apic/apic.cpp
	${CC}  $(CCPARAM) $(DEBUG) -c src/kernel/apic/apic.cpp -o obj/pic.o
else
obj/pic.o: 8259a.cpp
	${CC}  $(CCPARAM) $(DEBUG) -c src/kernel/driver/apic.cpp -o obj/pic.o
endif

obj/%o : src/%asm
	mkdir -p $(@D)
	nasm -f elf64  -g -o $@ $<


obj/%.o : src/%.s
	mkdir -p $(@D)
	${YASM} $(DEBUG) -64 $< -o $@


obj/%.s : src/%.S
	mkdir -p $(@D)
	${CC} -E $(DEBUG)  %.S > %.s

obj/%.bin : src/%.asm
	mkdir -p $(@D)
	${NASM} $(DEBUG) $< -o $@

system : ${objects}
	ld ${ASPARAM}  -o $@ ${objects} -T  kernel.lds 

all : system
	objcopy -I elf64-x86-64 -O binary system kernel.bin
	nm system | sort > system.map
 

clean :
	rm -rf obj/* boot.img system

install-floopy :  ${bins} ${objects} all 
	rm -rf boot.img
	rm -rf *.lock
	rm -rf .bochsrc
	sudo cp .bochsrc-floopy .bochsrc
	make -C ./user/ all
	cp boot1.img boot.img
	dd if=obj/bootloader/boot.bin of=./boot.img bs=512 count=1 conv=notrunc
	sudo mount ./boot.img /media/ -t vfat -o loop
	sudo cp obj/bootloader/loader.bin /media/
	sudo cp kernel.bin /media/
	sync
	sudo umount /media/
	objdump -d -mi386:x86-64:intel system > a.log
	nm system | awk '{ print $1" "$3 }'  > loader.sym

	# sudo rm -rf *.bin *.o system


install-disk :  ${bins} ${objects} all 
	rm -rf boot.img
	rm -rf *.lock
	rm -rf .bochsrc
	sudo cp .bochsrc-disk .bochsrc
	make -C ./user/ all
	cp boot2.img boot.img
	dd if=obj/bootloader/boot.bin of=./boot.img bs=512 count=1 conv=notrunc
	sudo mount ./boot.img /media/ -t vfat -o loop
	sudo cp obj/bootloader/loader.bin /media/
	sudo cp kernel.bin /media/
	sync
	sudo umount /media/
	objdump -d -mi386:x86-64:intel system > a.log
	nm system | awk '{ print $1" "$3 }'  > loader.sym

	# sudo rm -rf *.bin *.o system


run : install-floopy
	bochs -f .bochsrc && 6
	

quemu-disk:install-disk
	qemu-system-x86_64  ./boot.img   -m 2048

qemu-floopy: install-floopy
	qemu-system-x86_64  -fda boot.img  -m 2048
	# qemu-system-x86_64  -driverfile=boot.img  of=floopy  -m 2048

qemu-gdb-floopy: install-floopy
	qemu-system-x86_64 \
	-drive file=boot3.img,index=0,media=disk,if=ide \
	-drive file=boot4.img,index=1,media=disk,if=ide  \
	-drive file=ata1_master.img,index=2,media=disk,if=ide  \
	-vga std \
	-cpu  Nehalem-IBRS,+x2apic,+vmx \
	-fda boot.img \
	-enable-kvm \
	-M pc-i440fx-2.9 \
	-boot order=a \
	-m 4096 \
	-smp 1 \
	-s -S \
	# -nographic \
	

qemu-gdb-disk: install-disk
	qemu-system-x86_64  ./boot.img -s -S  -m 2048

gdb: 
	gdb \
	-ex 'set architecture i386:x86-64' \
	-ex 'target remote localhost:1234'  \
	-ex 'set disassembly-flavor intel' \
	-ex 'layout asm' \
	-ex 'layout regs' \
	-ex 'hb *0x7c00' \
	-ex 'continue' \
	-ex 'ni'





TESATBINS = obj/boot-test.bin\
			obj/loader-test.bin

		 

obj/%.bin : src/bootloader/%.asm
	mkdir -p $(@D)
	${NASM} $< -o $@

test: ${TESATBINS}
	rm -rf boot.img
	rm -rf *.lock
	rm -rf .bochsrc
	sudo cp .bochsrc-disk .bochsrc
	cp boot2.img boot.img
	dd if=obj/boot-test.bin of=./boot.img bs=512 count=1 conv=notrunc
	dd if=obj/loader-test.bin of=./boot.img seek=2 bs=512 count=4 conv=notrunc


test-run-qemu: test
	qemu-system-x86_64 \
	-s -S \
	-drive file=boot3.img,index=1,media=disk,if=ide  \
	-drive file=ata1_master.img,index=2,media=disk,if=ide  \
	-drive file=boot.img,index=0,media=disk,if=ide \
	-vga std \
	-m 2048 \

test-run-bochs: test
	bochs -f .bochsrc 

# 获取文件长度并保存到环境变量
test2:
	export systemsize=$(du -b  system | awk '{printf (($1 - ($1%512)) / 512 ) + 1}')
	dd if=./system of=./boot.img bs=512 count=${systemsize} conv=notrunc

